package com.global.college.base.baselist;

import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.global.college.R;
import com.global.college.base.BaseActivity;
import com.global.college.constants.Constant;
import com.global.college.widget.HnLoadingLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;


/**
 * 类描述：列表的公共类
 */
public abstract class CommListActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {

    protected RecyclerView mRecycler;
    protected SmartRefreshLayout mSpring;
    protected HnLoadingLayout mLoadingLayout;

    public CommRecyclerAdapter mAdapter;
    public int page = Constant.page;
    public int pageSize = Constant.pageSize;

    private boolean isGrid = false;

    @Override
    public int getContentViewId() {
        return R.layout.comm_list;
    }

    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        mRecycler = findViewById(R.id.mRecycler);
        mSpring = findViewById(R.id.smartRefreshLayout);
        mSpring.setOnRefreshListener(this);
        mSpring.setOnLoadmoreListener(this);
        pageSize = Constant.pageSize;
        mLoadingLayout = findViewById(R.id.mHnLoadingLayout);
        //适配器
        getData();
        //适配器
        mAdapter = setAdapter();
        initRecycler();
    }

    @Override
    public void getInitData() {

    }

    /**
     * 加载数据
     */
    protected abstract void getData();

    /**
     * 初始化适配器
     * @return
     */
    protected abstract CommRecyclerAdapter setAdapter();

    public void setGridManager(boolean isGrid) {
        this.isGrid = isGrid;

    }


    /**
     * 方法描述：Recycler列表初始化
     */
    private void initRecycler() {
        if (isGrid) {
            mRecycler.setLayoutManager(new GridLayoutManager(this, 2));
            mRecycler.addItemDecoration(new HnRecyclerGridDecoration(8));
        } else {
            mRecycler.setLayoutManager(new LinearLayoutManager(this));
        }

        mRecycler.setHasFixedSize(true);
        mRecycler.setAdapter(mAdapter);
    }

    protected void setEmpty(String content, int res) {
        if (mAdapter == null) return;
        if (mAdapter.getItemCount() < 1) {
            mLoadingLayout.setStatus(HnLoadingLayout.Empty);
            mLoadingLayout.setEmptyText(content).setEmptyImage(res);
        } else {
            mLoadingLayout.setStatus(HnLoadingLayout.Success);
        }
    }

    /**
     * 停下下拉刷新
     */
    protected void refreshFinish() {
        if (mSpring != null) {
            mSpring.finishRefresh(500);
            mSpring.resetNoMoreData();
        }
    }

    /**
     * 停止加载更多
     */
    protected void finishLoadmore() {
        if (mSpring != null) {
            mSpring.finishLoadmore(500);
        }
    }

    /**
     * 停止加载，没有更多数据
     */
    protected void finishLoadmoreWithNoMoreData() {
        if (mSpring != null) {
            mSpring.finishLoadmoreWithNoMoreData();
        }
    }


}
